package umg.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Authenticator;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.NoRouteToHostException;
import java.net.ProtocolException;
import java.net.Proxy;
import java.net.SocketAddress;
import java.net.SocketException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.net.UnknownHostException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import umg.Constants;
import umg.Messages;
import umg.gui.GUI;
import umg.vo.MatchSnapShotVO;
import umg.vo.PlayerVO;

public class HTMLStreamUtils
{
	static URLConnection[] urlConnection = null;// one for each server
	static URLConnection[] chatUrlConnection = null;// for chat

	static Authenticator myAuthenticator;

	public HTMLStreamUtils()
	{
		super();
		urlConnection = new URLConnection[PropertyManager.getWebadminURLs().length];
		chatUrlConnection = new URLConnection[PropertyManager.getWebadminURLs().length];
	}

	public static MatchSnapShotVO getMatchSnapShot(int url_number, long scheduledExecutionTime)
	{
		if (PropertyManager.isDebug()) System.out.println(Messages.getString("HTMLStreamUtils.0") + url_number);
		// Install the custom HTTP authenticator
		if (myAuthenticator == null)
		{
			myAuthenticator = new MyAuthenticator();
		}
		Authenticator.setDefault(myAuthenticator);
		if ((System.currentTimeMillis() - scheduledExecutionTime) >= (Constants.MAX_TARDINESS * (url_number + 1)))
		{
			System.out.println(Messages.getString("HTMLStreamUtils.1") + url_number);
			return null; // Too late, skip this execution.
		}
		if (openURLConnection(url_number) == -1) return null;
		if ((System.currentTimeMillis() - scheduledExecutionTime) >= (Constants.MAX_TARDINESS * (url_number + 1)))
		{
			System.out.println(Messages.getString("HTMLStreamUtils.1") + url_number);
			return null; // Too late, skip this execution.
		}
		MatchSnapShotVO matchSnapShotVO = createMatchSnapshot(url_number, scheduledExecutionTime);
		if (PropertyManager.isDebug()) System.out.println(Messages.getString("HTMLStreamUtils.3") + url_number);
		closeURLConnection(url_number);
		return matchSnapShotVO;
	}

	static DateFormat dateFormat = new SimpleDateFormat(Messages.getString("HTMLStreamUtils.4"));

	/* removed sync */
	/** #1 most executed method */
	public static String[] getChatLinesHTML(int url_number, long scheduledExecutionTime)
	{
		String[] lines = null;
		String str;
		BufferedReader htmlstream;
		if ((System.currentTimeMillis() - scheduledExecutionTime) >= (Constants.MAX_TARDINESS_CHAT * (url_number + 1)))
		{
			System.out.println(Messages.getString("HTMLStreamUtils.5") + url_number + Messages.getString("HTMLStreamUtils.6") + (System.currentTimeMillis() - scheduledExecutionTime)); //$NON-NLS-2$
			return null; // Too late, skip this execution.
		}

		// Install the custom HTTP authenticator
		if (myAuthenticator == null)
		{
			myAuthenticator = new MyAuthenticator();
		}
		Authenticator.setDefault(myAuthenticator);
		chatUrlConnection[url_number] = null;
		if (openChatURLConnection(url_number) == -1) return null;// error
		// getting
		// connection
		if ((System.currentTimeMillis() - scheduledExecutionTime) >= (Constants.MAX_TARDINESS_CHAT * (url_number + 1)))
		{
			System.out.println(Messages.getString("HTMLStreamUtils.7") + url_number);
			return null; // Too late, skip this execution.
		}
		try
		{
			chatUrlConnection[url_number].setConnectTimeout(Constants.CHAT_CONNECTION_TIMEOUT);
			InputStream inputStream = chatUrlConnection[url_number].getInputStream();
			if ((System.currentTimeMillis() - scheduledExecutionTime) >= (Constants.MAX_TARDINESS_CHAT * (url_number + 1)))
			{
				if (inputStream != null) inputStream.close();
				inputStream = null;
				System.out.println(Messages.getString("HTMLStreamUtils.8") + url_number + Messages.getString("HTMLStreamUtils.9") + (System.currentTimeMillis() - scheduledExecutionTime)); //$NON-NLS-2$
				return null; // Too late, skip this execution.
			}
			InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
			htmlstream = new BufferedReader(inputStreamReader);
			while ((str = htmlstream.readLine()) != null)
			{
				// chats start with ">",all on one line
				// sample: &gt; est1<br>&gt; testr<br>
				if (str.startsWith(Messages.getString("HTMLStreamUtils.10")))
				{
					str = str.substring(5);// skip over "&gt; "
					lines = str.split(Messages.getString("HTMLStreamUtils.11") + Constants.SPACE_STR);
					if (lines.length > 0 && lines[0] != null)
					{
						// lines[0]=lines[0].substring(5,lines[0].length());//
						// remove
						// the pesky > on the first line
						// go through all the lines and look for
						// "WebAdmin:&nbsp;GAME&nbsp;HAS&nbsp;ENDED"
						// add time string to it if found.
						for (int i = 0; i < lines.length; i++)
						{
							if (lines[i].endsWith(Messages.getString("HTMLStreamUtils.12")))
							{
								lines[i] = lines[i].substring(0, lines[i].length() - 4);// remove
								// the
								// pesky
								// <br>
								// on
								// the
								// last
								// line
							}
							if (lines[i].startsWith(Messages.getString("HTMLStreamUtils.13")))
							{
								lines[i] = Messages.getString("HTMLStreamUtils.14") + dateFormat.format(new Date());//+ Messages.getString("HTMLStreamUtils.15"); //$NON-NLS-2$
								// lines[i]="<br/>Game Ended at "+dateFormat.
								// format(new
								// Date());
								break;
							}
						}

						// we got the line containing all the chat lines and
						// broke them up into an array of strings.
						break;
					}
				}

			}// while
			htmlstream.close();
			htmlstream = null;
			inputStream.close();
			inputStream = null;
		}
		catch (IOException e)
		{
			if (e instanceof NoRouteToHostException)
			{
				GUI.logToStatus(PropertyManager.getWebadminURLNames()[url_number]+": "+Messages.getString("HTMLStreamUtils.16") + PropertyManager.getWebadminURLNames()[url_number] + Messages.getString("HTMLStreamUtils.17")); //$NON-NLS-2$
			}
			else if (e instanceof UnknownHostException)
			{
				GUI.logToStatus(PropertyManager.getWebadminURLNames()[url_number]+": "+Messages.getString("HTMLStreamUtils.18") + e.getMessage());
			}
			else
			{
				GUI.logToStatus(PropertyManager.getWebadminURLNames()[url_number]+": "+e.getMessage());
				// e.printStackTrace();
			}
		}
		return lines;
	}

	private static int openChatURLConnection(int url_number)
	{
		URL url;
		String playerurl, chaturl = Messages.getString("HTMLStreamUtils.19");
		if (chatUrlConnection[url_number] != null)
		{
			return 0;// have a connection
		}

		try
		{
			playerurl = PropertyManager.getWebadminURLs()[url_number];
			chaturl = playerurl.substring(0, playerurl.lastIndexOf(Messages.getString("HTMLStreamUtils.20"))) + Messages.getString("HTMLStreamUtils.21"); //$NON-NLS-2$
			// replace currnet_players with current_console_log to get the
			// console log page
			url = new URL(chaturl);
			// connect with a proxy if needed
			if (PropertyManager.getProxy_enabled().equals(Messages.getString("HTMLStreamUtils.22")))
			{
				SocketAddress addr = new InetSocketAddress(PropertyManager.getProxyHost(), Integer.parseInt(PropertyManager.getProxy_port()));
				Proxy proxy = new Proxy(Proxy.Type.HTTP, addr);
				chatUrlConnection[url_number] = (HttpURLConnection) url.openConnection(proxy);
			}
			else
			{
				chatUrlConnection[url_number] = url.openConnection();
			}
			if (PropertyManager.isDebug()) System.out.println(Messages.getString("HTMLStreamUtils.23") + chaturl);
			return 0;
			// System.out.println(url_number+" Connected to: " + chaturl);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			System.out.println(PropertyManager.getWebadminURLNames()[url_number]+": "+Messages.getString("HTMLStreamUtils.24") + e.getMessage());
			System.out.println(PropertyManager.getWebadminURLNames()[url_number]+": "+Messages.getString("HTMLStreamUtils.25") + PropertyManager.getWebadminURLNames()[url_number]);
		}
		return -1;
	}

	public static void closeURLConnection(int index)
	{
		try
		{
			// needed cause we have an offline mode that uses a plain
			// urlconenction with no disconnect option
			if (urlConnection[index] instanceof HttpURLConnection)
			{
				((HttpURLConnection) urlConnection[index]).disconnect();
			}
		}
		catch (Exception e)
		{
		}
		finally
		{
			urlConnection[index] = null;
		}
	}

	/**
	 * create a MatchSnapShotVO object using the data obtained from the
	 * connection 'urlConnection' removed sync.
	 *
	 * @return MatchSnapShotVO #2 most executed method
	 */
	private static MatchSnapShotVO createMatchSnapshot(int url_number, long scheduledExecutionTime)
	{
		// Read all the text returned by the server
		BufferedReader htmlstream = null;
		String str;
		Date timeStamp = new Date();
		MatchSnapShotVO matchSnapShotVO = new MatchSnapShotVO();
		matchSnapShotVO.setTimestamp(timeStamp);// for the snapshot
		PlayerVO players[] = new PlayerVO[Constants.MAXPLAYERS];
		PlayerVO player = null;
		int counter = 0;
		try
		{
			if (urlConnection[url_number] == null) openURLConnection(url_number);
			urlConnection[url_number].setConnectTimeout(Constants.PLAYER_CONNECTION_TIMEOUT);
			InputStream inputStream = urlConnection[url_number].getInputStream();
			if (System.currentTimeMillis() - scheduledExecutionTime >= (Constants.MAX_TARDINESS * (url_number + 1)))
			{
				if (inputStream != null) inputStream.close();
				inputStream = null;
				return null; // Too late, skip this execution.
			}
			InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
			htmlstream = new BufferedReader(inputStreamReader);
			while ((str = htmlstream.readLine()) != null)
			{
				// str is one line of text; readLine() strips the newline
				// character(s)
				if (str.trim().startsWith(Constants.START_PLAYER_DATA_BLOCK))
				{
					String[] dataStrings = new String[20];
					int i = 0;
					dataStrings[i] = str;
					while ((str = htmlstream.readLine()) != null)
					{// read lines till "</tr>"
						i++;
						dataStrings[i] = str;
						if (str.startsWith(Constants.END_PLAYER_DATA_BLOCK))
						{
							break;
						}
					}
					// read a datablock, process it.
					player = processDataBlock(dataStrings);
					player.setTimestamp(timeStamp);
					players[counter++] = player;
					// System.out.println(player.toString());
				}// read this players data
					// do nothing with other data
			}// while
			htmlstream.close();
			htmlstream = null;
			inputStream.close();
			inputStream = null;
		}
		catch (IOException e1)
		{
			if (e1 instanceof ProtocolException && e1.getMessage().startsWith(Messages.getString("HTMLStreamUtils.26")))
			{
				GUI.logToStatus(PropertyManager.getWebadminURLNames()[url_number]+": "+Messages.getString("HTMLStreamUtils.27"));
				GUI.getUsernamePassword();
				GUI.logToStatus(PropertyManager.getWebadminURLNames()[url_number]+": "+Messages.getString("HTMLStreamUtils.28"));
			}
			else if (e1 instanceof NoRouteToHostException)
			{
				GUI.logToStatus(PropertyManager.getWebadminURLNames()[url_number]+": "+Messages.getString("HTMLStreamUtils.29"));
			}
			else if (e1 instanceof SocketException)
			{
				GUI.logToStatus(PropertyManager.getWebadminURLNames()[url_number]+": "+e1.getMessage());
			}
			else if (e1 instanceof UnknownHostException)
			{
				GUI.logToStatus(PropertyManager.getWebadminURLNames()[url_number]+": "+Messages.getString("HTMLStreamUtils.30") + e1.getMessage());
			}
			else if (e1 instanceof ConnectException)
			{
				GUI.logToStatus(PropertyManager.getWebadminURLNames()[url_number]+": "+Messages.getString("HTMLStreamUtils.31") + e1.getMessage());
			}
			else
			{// nullpointer possibly
				if (PropertyManager.isDebug()) e1.printStackTrace();
				System.out.println(Messages.getString("HTMLStreamUtils.24") + e1.getMessage());
				GUI.logToStatus(PropertyManager.getWebadminURLNames()[url_number]+": "+Messages.getString("HTMLStreamUtils.33") + e1.getMessage());
			}
		}
		finally
		{
			try
			{
				closeURLConnection(url_number);
				htmlstream.close();
			}
			catch (Exception e)
			{
				// e.printStackTrace();
			}
		}
		PlayerVO notnullplayers[] = new PlayerVO[counter];
		for (int i = 0; i < counter; i++)
		{
			notnullplayers[i] = players[i];
		}
		matchSnapShotVO.setPlayers(notnullplayers);
		return matchSnapShotVO;
		// System.out.println(matchSnapShotVO.toString());
	}

	private static PlayerVO processDataBlock(String[] dataStrings)
	{
		String str = Messages.getString("HTMLStreamUtils.19");
		boolean foundPing = false;
		boolean gotip = false;
		PlayerVO playerVO = new PlayerVO();

		try
		{// catch any exceptions and ignore them :(
			for (int i = 0; i < dataStrings.length && dataStrings[i] != null; i++)
			{
				str = dataStrings[i];
				if (str.indexOf(Constants.PLAYERNAME_PATTERN) != -1)
				{// name
					String player = Messages.getString("HTMLStreamUtils.19");
					int beginIndex, endIndex;
					beginIndex = str.indexOf(Constants.PLAYERNAME_PATTERN) + Constants.PLAYERNAME_PATTERN.length();
					endIndex = str.indexOf(Constants.ENDDATA_PATTERN);
					player = str.substring(beginIndex, endIndex);
					if (player.indexOf(Constants.SPEC_APPENDED_STR) != -1)
					{// (Spectator) is appended, remove it;
						player = player.substring(0, player.indexOf(Constants.SPEC_APPENDED_STR));
					}
					playerVO.setPlayerName(StringUtils.unescapeHTML(player, 0));
					// System.out.println("Player: "+player);
				}
				else if (str.indexOf(Constants.TEAM_PATTERN) != -1)
				{// team
					String team = Messages.getString("HTMLStreamUtils.19");
					int beginIndex, endIndex;
					beginIndex = str.indexOf(Constants.TEAM_PATTERN) + Constants.TEAM_PATTERN.length();
					endIndex = str.indexOf(Constants.ENDDATA_PATTERN);
					team = str.substring(beginIndex, endIndex);
					if (team.indexOf(Constants.RED_TEAM_PATTERN) != -1)
					{
						team = Constants.RED_STR;
					}
					else if (team.indexOf(Constants.BLUE_TEAM_PATTERN) != -1)
					{
						team = Constants.BLUE_STR;
					}
					else
					{
						team = Constants.SPEC_STR;
					}
					playerVO.setTeam(team);
					// System.out.println("Team: "+team);
				}
				else if (str.indexOf(Constants.IP_PATTERN) != -1 && gotip == false)
				{// ip
					String ip = Messages.getString("HTMLStreamUtils.19");
					int beginIndex, endIndex;
					beginIndex = str.indexOf(Constants.IP_PATTERN) + Constants.IP_PATTERN.length();
					endIndex = str.indexOf(Constants.ENDDATA_PATTERN);
					ip = str.substring(beginIndex, endIndex);
					if (ip.equals(Messages.getString("HTMLStreamUtils.38")))
					{
						ip = Messages.getString("HTMLStreamUtils.39");// bot
					}
					playerVO.setIP(ip);
					gotip = true;
					// System.out.println("IP: "+ip+"\n");
				}
				else if (str.indexOf(Constants.IP_PATTERN) != -1 && gotip == true)
				{// global id follows IP column
					String guid = Messages.getString("HTMLStreamUtils.19");
					int beginIndex, endIndex;
					beginIndex = str.indexOf(Constants.IP_PATTERN) + Constants.IP_PATTERN.length();
					endIndex = str.indexOf(Constants.ENDDATA_PATTERN);
					guid = str.substring(beginIndex, endIndex);
					if (guid.equals(Messages.getString("HTMLStreamUtils.38")))
					{
						guid = Messages.getString("HTMLStreamUtils.19");// bot
					}
					if (guid != null && guid.length() > 0)
					{
						playerVO.setGlobalID(guid);
						System.out.println("For " + playerVO.getPlayerName() + ", Global ID: " + guid + "\n");
					}
				}// else
				else if (str.startsWith(Constants.PING_SCORE_PATTERN) && str.indexOf(Messages.getString("HTMLStreamUtils.43")) == -1)
				{// Ping OR Score, Ping is first
					if (foundPing == false)
					{
						String ping = Messages.getString("HTMLStreamUtils.19");
						int beginIndex, endIndex;
						beginIndex = str.indexOf(Constants.PING_SCORE_PATTERN) + Constants.PING_SCORE_PATTERN.length();
						endIndex = str.indexOf(Constants.ENDDATA_PATTERN);
						ping = str.substring(beginIndex, endIndex);
						if (ping.equals(Messages.getString("HTMLStreamUtils.19"))) ping = Messages.getString("HTMLStreamUtils.46"); //$NON-NLS-2$
						playerVO.setPing(ping);
						// System.out.println("Ping: "+ping);
						foundPing = true;
					}
					else
					{// ping was found, this must be the score
						String score = Messages.getString("HTMLStreamUtils.19");
						int beginIndex, endIndex;
						beginIndex = str.indexOf(Constants.PING_SCORE_PATTERN) + Constants.PING_SCORE_PATTERN.length();
						endIndex = str.indexOf(Constants.ENDDATA_PATTERN);
						score = str.substring(beginIndex, endIndex);
						playerVO.setScore(score);
						// System.out.println("Score: "+score);
					}

				}// else
				else if (str.indexOf(Messages.getString("HTMLStreamUtils.48")) > -1)
				{// find Kick#
					int beginIndex = str.indexOf(Messages.getString("HTMLStreamUtils.49")) + 10;
					int endIndex = str.indexOf(Messages.getString("HTMLStreamUtils.50"));
					String webID = str.substring(beginIndex, endIndex);
					playerVO.setWebID(webID);
				}
			}// for
		}
		catch (Exception e)
		{
			return null;
		}
		return playerVO;
	}// processDataBlock

	private static int openURLConnection(int url_number)
	{
		URL url;
		if (urlConnection[url_number] != null)
		{
			return 0;// have a connection
		}

		try
		{
			url = new URL(PropertyManager.getWebadminURLs()[url_number]);
			// connect with a proxy if needed
			if (PropertyManager.getProxy_enabled().equals(Messages.getString("HTMLStreamUtils.22")))
			{
				SocketAddress addr = new InetSocketAddress(PropertyManager.getProxyHost(), Integer.parseInt(PropertyManager.getProxy_port()));
				Proxy proxy = new Proxy(Proxy.Type.HTTP, addr);
				urlConnection[url_number] = (HttpURLConnection) url.openConnection(proxy);
			}
			else
			{
				urlConnection[url_number] = url.openConnection();
			}
			if (PropertyManager.isDebug()) System.out.println(Messages.getString("HTMLStreamUtils.52") + PropertyManager.getWebadminURLNames()[url_number]);
			return 0;
		}
		catch (MalformedURLException e)
		{
			e.printStackTrace();
			System.out.println(Messages.getString("HTMLStreamUtils.24") + e.getMessage());
		}
		catch (IOException e)
		{
			e.printStackTrace();
			System.out.println(Messages.getString("HTMLStreamUtils.24") + e.getMessage());
			System.out.println(Messages.getString("HTMLStreamUtils.55") + PropertyManager.getWebadminURLNames()[url_number]);
		}
		return -1;
	}

	public static void banID(int url_number, String id)
	{

		if (id == null || id.length() == 0) return;

		URL url;
		// Install the custom HTTP authenticator
		if (myAuthenticator == null)
		{
			myAuthenticator = new MyAuthenticator();
		}
		Authenticator.setDefault(myAuthenticator);
		HttpURLConnection connection = null;

		try
		{
			// URL TO KICK
			//http://74.63.66.150:5080/ServerAdmin/current_players?[Kick|Ban]31=
			// True
			String playerURL = PropertyManager.getWebadminURLs()[url_number];
			// remove text after "ServerAdmin/" and append
			// "current_players?Kick"
			String sendTextURL = playerURL.substring(0, playerURL.indexOf(Messages.getString("HTMLStreamUtils.56")) + 12);
			sendTextURL += Messages.getString("HTMLStreamUtils.57") + id + Messages.getString("HTMLStreamUtils.58"); //$NON-NLS-2$
			url = new URL(sendTextURL);
			// connect with a proxy if needed
			if (PropertyManager.getProxy_enabled().equals(Messages.getString("HTMLStreamUtils.22")))
			{
				SocketAddress addr = new InetSocketAddress(PropertyManager.getAutokickPlayerEnabled(), Integer.parseInt(PropertyManager.getProxy_port()));
				Proxy proxy = new Proxy(Proxy.Type.HTTP, addr);
				connection = (HttpURLConnection) url.openConnection(proxy);
			}
			else
			{
				connection = (HttpURLConnection) url.openConnection();
			}
			if (PropertyManager.isDebug()) System.out.println(Messages.getString("HTMLStreamUtils.60") + sendTextURL);
			connection.getContent();

		}
		catch (MalformedURLException e)
		{
			e.printStackTrace();
			System.out.println(Messages.getString("HTMLStreamUtils.24") + e.getMessage());
		}
		catch (IOException e)
		{
			e.printStackTrace();
			System.out.println(Messages.getString("HTMLStreamUtils.24") + e.getMessage());
			System.out.println(Messages.getString("HTMLStreamUtils.55") + PropertyManager.getWebadminURLNames()[url_number]);
		}

	}

	public static void kickID(int url_number, String id)
	{
		if (id == null || id.length() == 0) return;

		URL url;
		// Install the custom HTTP authenticator
		if (myAuthenticator == null)
		{
			myAuthenticator = new MyAuthenticator();
		}
		Authenticator.setDefault(myAuthenticator);
		HttpURLConnection connection = null;

		try
		{
			// URL TO KICK
			//http://74.63.66.150:5080/ServerAdmin/current_players?[Kick|Ban]31=
			// True
			String playerURL = PropertyManager.getWebadminURLs()[url_number];
			// remove text after "ServerAdmin/" and append
			// "current_players?Kick"
			String sendTextURL = playerURL.substring(0, playerURL.indexOf(Messages.getString("HTMLStreamUtils.64")) + 12);
			sendTextURL += Messages.getString("HTMLStreamUtils.65") + id + Messages.getString("HTMLStreamUtils.66"); //$NON-NLS-2$
			url = new URL(sendTextURL);
			// connect with a proxy if needed
			if (PropertyManager.getProxy_enabled().equals(Messages.getString("HTMLStreamUtils.22")))
			{
				SocketAddress addr = new InetSocketAddress(PropertyManager.getAutokickPlayerEnabled(), Integer.parseInt(PropertyManager.getProxy_port()));
				Proxy proxy = new Proxy(Proxy.Type.HTTP, addr);
				connection = (HttpURLConnection) url.openConnection(proxy);
			}
			else
			{
				connection = (HttpURLConnection) url.openConnection();
			}
			if (PropertyManager.isDebug()) System.out.println(Messages.getString("HTMLStreamUtils.60") + sendTextURL);
			connection.getContent();

		}
		catch (MalformedURLException e)
		{
			e.printStackTrace();
			System.out.println(Messages.getString("HTMLStreamUtils.24") + e.getMessage());
		}
		catch (IOException e)
		{
			e.printStackTrace();
			System.out.println(Messages.getString("HTMLStreamUtils.24") + e.getMessage());
			System.out.println(Messages.getString("HTMLStreamUtils.55") + PropertyManager.getWebadminURLNames()[url_number]);
		}
	}

	// send a "GET" request to the server with the passed in string as a
	// parameter
	// replace all spaces with +
	public static void sendText(int url_number, String text)
	{
		URL url;
		// Install the custom HTTP authenticator
		if (myAuthenticator == null)
		{
			myAuthenticator = new MyAuthenticator();
		}
		Authenticator.setDefault(myAuthenticator);

		HttpURLConnection tempSendTextConnection = null;

		try
		{
			String playerURL = PropertyManager.getWebadminURLs()[url_number];
			// remove text after "ServerAdmin/" and append
			// "current_console?SendText=say"
			String sendTextURL = playerURL.substring(0, playerURL.indexOf(Messages.getString("HTMLStreamUtils.72")) + 12);
			sendTextURL += Messages.getString("HTMLStreamUtils.73");
			sendTextURL += URLEncoder.encode(text, Messages.getString("HTMLStreamUtils.74"));
			// StringUtils.oldReplaceAll(text, " ", "+");
			url = new URL(sendTextURL);
			// connect with a proxy if needed
			if (PropertyManager.getProxy_enabled().equals(Messages.getString("HTMLStreamUtils.22")))
			{
				SocketAddress addr = new InetSocketAddress(PropertyManager.getAutokickPlayerEnabled(), Integer.parseInt(PropertyManager.getProxy_port()));
				Proxy proxy = new Proxy(Proxy.Type.HTTP, addr);
				tempSendTextConnection = (HttpURLConnection) url.openConnection(proxy);
			}
			else
			{
				tempSendTextConnection = (HttpURLConnection) url.openConnection();
			}
			if (PropertyManager.isDebug()) System.out.println(Messages.getString("HTMLStreamUtils.76") + sendTextURL);
			tempSendTextConnection.getContent();

			// tempSendTextConnection.disconnect();
			// tempSendTextConnection=null;
		}
		catch (MalformedURLException e)
		{
			e.printStackTrace();
			System.out.println(Messages.getString("HTMLStreamUtils.24") + e.getMessage());
		}
		catch (IOException e)
		{
			e.printStackTrace();
			System.out.println(Messages.getString("HTMLStreamUtils.24") + e.getMessage());
			System.out.println(Messages.getString("HTMLStreamUtils.55") + PropertyManager.getWebadminURLNames()[url_number]);
		}

	}

}
